import os
import queue


def findFile(workPath, fileType, dataQueue):
    print("正在查找文件")
    for home, dirs, files in os.walk(workPath):
        for filename in files:
            path = os.path.join(home, filename)
            # 屏蔽 MACOS 下 .DS_Store 文件
            if path.split(".")[-1] != "DS_Store":
                if fileType != "":
                    if path.split(".")[-1] == fileType:
                        dataQueue.put(path)
                else:
                    dataQueue.put(path)
            else:
                pass
    check()


def check():
    print("计算中")
    line = 0
    errorNum = 0
    code = 0
    allFiles = dataQueue.qsize()
    while True:
        if not dataQueue.empty():
            file = dataQueue.get()
            try:
                with open(file) as f:
                    text = f.read().replace("\t", "")
                    line += len(text.splitlines())
                    code += len(text)
            except:
                errorNum += 1
        else:
            print("执行完毕，共找到了 " + str(allFiles) + " 个 " + fileType + " 文件，约写了：" + str(line) + " 行代码，敲了" + str(
                code) + " 次键盘，" + str(
                errorNum) + " 个文件无法读取")
            break


if __name__ == '__main__':
    workPath = r"C:\Users\97987\PycharmProjects\GraduationThesis"
    fileType = "py"

    dataQueue = queue.Queue()
    findFile(workPath, fileType, dataQueue)
